################################################################################
# The SETTINGS CLASS 
#   A Graphical User Interface
#
#   Allows the user to set
#   *Default basic color for background & foreground
#   *Page color
#   *Fonts option
#   *Default author name
################################################################################
from Tkinter import *
from SettingsClass import *
from tkFileDialog import *
from tkColorChooser import askcolor
from FontChooser import *
from tkSimpleDialog import askstring
import tkMessageBox
import tkFont
import pickle
import os


class SettingsClass(object):

    def __init__(self, root, obj, loadedData):
        self.loadedData = loadedData
        
        self.root = root
        self.obj = obj
        self.childframe = Frame(self.root)
        self.index=0;
        self.theFont=self.loadedData["fonts"]
        self.iconsList=[]
        self.labelsList=[]
        self.createWidgets()
        self.root.grid()

    #Allow the user to toggle through available font choices
    #Adds the font choice to the return value as well
    def pickFonts(self, event):
        self.theFont = FontChooser(self.root, self.loadedData["fonts"]).returnvalue()
        self.loadedData["fonts"] = self.theFont
        self.settingLabel.config(font=(self.theFont, 36, 'bold'))
        self.myfonts = tkFont.Font(family=self.theFont, size=16, slant="italic")

        self.clearWidgets()
        self.createWidgets()
       
    #Obtain the user's color and set the return foreground color when
    #setting window closes.
    def changeFGcolor(self, event):
        result = askcolor(parent=self.root, initialcolor='#ff0000')[1]
        self.settingLabel.config(fg=result)
        self.loadedData["fg"] = result
        
        for lbl in self.labelsList:
            lbl.config(fg=result)
        
    #Obtain user's color selection and set the return background color when
    #setting window closes.
    def changeBGcolor(self, event):
        chosenColor = askcolor(parent=self.root, initialcolor='#ff0000')[1]
        self.loadedData["bg"]=chosenColor
        
        self.root.config(background=chosenColor)
        self.settingLabel.config(background=chosenColor)
        for item in self.labelsList:
            item.config(background=chosenColor)
        for icon in self.iconsList:
            icon.config(bg=chosenColor)

    #Closes the Settings Window when user clicks HOME
    def exitWindow(self, event):
        file = open("settings.txt", "w")
        pickle.dump(self.loadedData, open("settings.txt", "wb"), protocol=pickle.HIGHEST_PROTOCOL)

        self.root.destroy()
        
    #Obtain user name input and edit the user name label
    def setUserName(self, event):
        os.system('onboard&')
        name = askstring("Name", "Please enter your name:")
        
        os.system('killall onboard')
        if name != None:
            self.userName.config(text=name)
            self.loadedData["name"]=name

    def showHelp(self, event):
        tkMessageBox.showinfo("help", "To set your name, click on the my name icon")

    #Obtain the chosen path name and edit the path name
    def setPath(self, event):
        self.newPath = askdirectory()
        if len(self.newPath)>0:
            self.pathName.config(text=self.newPath)
            self.loadedData["path"]=self.newPath
        
    def clearWidgets(self):
        self.settingLabel.destroy()
        for items in self.iconsList:
            items.destroy()
        for lbl in self.labelsList:
            lbl.destroy()

        self.iconList=[]
        self.labelsList=[]

    #Creates the widgets for the Settings Window.
    def createWidgets(self):
        
        myBg=self.loadedData["bg"]
        myFg=self.loadedData["fg"]
        self.root.config(bg=myBg)
        
        self.myfonts = tkFont.Font(family=self.theFont, size=16, slant="italic")
        
        self.settingLabel = Label(self.root, text="My Settings", font=(self.theFont, 36, 'bold'),pady='10', padx='400',bg=myBg,fg=myFg)
        self.settingLabel.grid(row=0, column=0, columnspan=5)
        
        self.usersPic = PhotoImage(file="icons/users.gif")
        self.userIcon = Label(self.root, image=self.usersPic, padx='100', pady='5',bg=myBg)
        self.userIcon.image=self.usersPic
        self.userIcon.grid(row=1, column=0, columnspan=2)
        self.iconsList.append(self.userIcon)

        self.userName = Label(self.root, text=self.loadedData["name"], font=self.myfonts, pady='3',fg=myFg,bg=myBg)
        self.userName.grid(row=1, column=1,columnspan=3)
        self.userName.bind("<Button-1>", self.setUserName)

        self.labelsList.append(self.userName)

        self.usersLabel = Label(self.root, text="my info", font=self.myfonts, padx='3', pady='2',fg=myFg,bg=myBg)
        self.usersLabel.grid(row=2, column=0, columnspan=2)
        self.usersLabel.bind("<Button-1>", self.setUserName)
        self.labelsList.append(self.usersLabel)
        
        self.space2 = Label(self.root, padx='225',bg=myBg)
        self.space2.grid(row=3, column=0, columnspan=3)
        self.labelsList.append(self.space2)

        self.pathPic = PhotoImage(file="icons/folder.gif")
        self.pathIcon = Label(self.root, image=self.pathPic, padx='30', pady='20',bg=myBg)
        self.pathIcon.image = self.pathPic
        self.pathIcon.bind("<Button-1>", self.setPath)
        self.pathIcon.grid(row=4, column=0, columnspan = 2)
        self.iconsList.append(self.pathIcon)

        self.pathName=Label(self.root, text=self.loadedData["path"], font=self.myfonts, padx='30', pady='10',  anchor=W, justify=LEFT, wraplength=450,fg=myFg,bg=myBg)
        self.pathName.grid(row=4, column=1,columnspan=3)
        self.labelsList.append(self.pathName)


        self.pathLabel=Label(self.root, text="my directory",font= self.myfonts, padx='30', pady='2',fg=myFg,bg=myBg)
        self.pathLabel.bind("<Button-1>", self.setPath)
        self.pathLabel.grid(row=5, column=0, columnspan=2)
        self.labelsList.append(self.pathLabel)


        self.space3 = Label(self.root,  padx='225', bg=myBg)
        self.space3.grid(row=6, column=0, columnspan=5)
        self.labelsList.append(self.space3)

        self.space4 = Label(self.root,  padx='225', bg=myBg)
        self.space4.grid(row=7, column=0, columnspan=5)
        self.labelsList.append(self.space4)

        self.fontsPic = PhotoImage(file="icons/edit_text.gif")
        self.fontIcon = Label(self.root,image=self.fontsPic, pady='5',bg=myBg)
        self.fontIcon.image = self.fontsPic
        self.fontIcon.bind("<Button-1>", self.pickFonts)
        self.fontIcon.grid(row=8, column=3, columnspan=2)
        self.iconsList.append(self.fontIcon)

        self.fgPic = PhotoImage(file="icons/brushcolor.gif")
        self.fgIcon = Label(self.root,image=self.fgPic, pady='5',bg=myBg)
        self.fgIcon.image = self.fgPic
        self.fgIcon.bind("<Button-1>", self.changeFGcolor)
        self.fgIcon.grid(row=8, column=1, columnspan=2)
        self.iconsList.append(self.fgIcon)

        self.backgroundPic = PhotoImage(file="icons/bg_color.gif")
        self.bgIcon = Label(self.root,image=self.backgroundPic, pady='5',bg=myBg)
        self.bgIcon.image = self.backgroundPic
        self.bgIcon.bind("<Button-1>", self.changeBGcolor)
        self.bgIcon.grid(row=8, column=2, columnspan=2)
        self.iconsList.append(self.bgIcon)

        self.homeLabel = Label(self.root, text="home", font=self.myfonts, pady='2',fg=myFg,bg=myBg)
        self.homeLabel.bind("<Button-1>", self.exitWindow)
        self.homeLabel.grid(row=9, column=0, columnspan=2)
        self.labelsList.append(self.homeLabel)

        self.homePic = PhotoImage(file="icons/home.gif")
        self.homeIcon = Label(self.root, image=self.homePic,pady='5',bg=myBg)
        self.homeIcon.image=self.homePic
        self.homeIcon.bind("<Button-1>", self.exitWindow)
        self.homeIcon.grid(row=8, column=0, columnspan=2)
        self.iconsList.append(self.homeIcon)

        self.fgLabel = Label(self.root, text="my text color", font=self.myfonts, pady='2',fg=myFg,bg=myBg)
        self.fgLabel.bind("<Button-1>", self.changeFGcolor)
        self.fgLabel.grid(row=9, column=1, columnspan=2)
        self.labelsList.append(self.fgLabel)

        self.bgLabel = Label(self.root, text="my background",font=self.myfonts, padx='5', pady='3',fg=myFg,bg=myBg)
        self.bgLabel.bind("<Button-1>", self.changeBGcolor)
        self.bgLabel.grid(row=9, column=2, columnspan=2)
        self.labelsList.append(self.bgLabel)

        self.fontLabel = Label(self.root, text="my fonts", font=self.myfonts, padx='5', pady='3',fg=myFg,bg=myBg)
        self.fontLabel.grid(row=9, column=3, columnspan=2)
        self.labelsList.append(self.fontLabel)
